const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");


// 未配送单据
router.post('/deliver/unDeliver', async ctx => {
    let tenantId = ctx.user.tenantId;
    let list = await db.models['cargo'].findAll({
        where: {
            tenantId: tenantId,
            state: 1,
        },
        order: [['carsId']]
    })
    let cargoIds = [];
    list.map(row => {
        cargoIds.push(row.id)
    })

    if (cargoIds.length === 0) {
        return ctx.err("没有待配送单据");
    }
    let list1 = tool.unDeliver(cargoIds.join(','));
    for (let i = 0; i < list.length; i++) {
        let list2 = []
        let cargo = list[i]
        for (let j = 0; j < list1.length; j++) {
            let it = list1[j]
            if (cargo.id === it.cargoId) {
                list2.push(it);
            }
            list[i].cargo = list2;
        }
    }
    return ctx.suc('', list);
})


module.exports = router;
